/**
 * Created by lwy on 2017/8/21.
 */


var page = 1;


function doAjax(){
    $.ajax({
        url:'/crm/agentApplyC/search.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            pageNu:page,
            name:$('#name').val(),
            phone:$('#phone').val(),
            city:$('#city').val(),
            identity:$('#identity').val(),
            state:$('#state').val(),
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.datas.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.prizeList.length;i++){
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1">'+data.datas.prizeList[i].id+'</td></a>'+
                        '<td class=" ">'+data.datas.prizeList[i].name+'</td>'+
                        '<td class=" ">'+data.datas.prizeList[i].phone+'</td>'+
                        '<td class=" ">'+data.datas.prizeList[i].identity+'</td>'+
                        '<td class="sorting_1">'+data.datas.prizeList[i].city+'</td>'+
                        '<td class=" ">'+data.datas.prizeList[i].createTime+'</td>'+
                        '<td class=" ">'+data.datas.prizeList[i].handTime+'</td>'+
                        '<td class=" ">'+data.datas.prizeList[i].handler+'</td>'+
                        '<td class="center">'+data.datas.prizeList[i].state+'</td>'+
                        '<td class="center withdrawId" id="'+data.datas.prizeList[i].id+'" style="text-decoration: underline;cursor: pointer;">处理</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }

                //id处理
                $('.withdrawId').unbind().bind('click',function(){
                    var r=confirm("您确定已处理过了吗？")
                    if (r==true)
                    {
                        $.ajax({
                            url:'/crm/agentApplyC/edit.json',
                            dataType:'json',
                            data:{
                                token:localStorage.getItem('crmtoken'),
                                id:$(this).attr('id'),
                            },
                            type:'post',
                            success:function(data){
                                if(data.code==1){
                                    alert('处理成功');
                                    page=1;
                                    doAjax();
                                }
                                if(data.code==-1){
                                    window.parent.location.href="/crm/login.jsp"
                                }
                                if(data.code==0){
                                    alert(data.msg);
                                }
                            }
                        })
                    }
                    else
                    {

                    }
                })

                $(".pagination").createPage({
                    totalPage:data.datas.totalPage,
                    currPage:data.datas.pageNu,
                    pageCount:data.datas.pageNu,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}

$(function(){
    doAjax();
})

//查询
$('#refer').click(function(){
    page=1;
    doAjax();
})
